<?php

/**
 * This is the model class for table "{{create_city}}".
 *
 * The followings are the available columns in table '{{create_city}}':
 * @property string $id
 * @property string $cn_name
 * @property string $en_name
 * @property string $province
 * @property integer $status
 */
class CreateCity extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{create_city}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('cn_name', 'required','message'=>'-1','on'=>'create,update'),//城市名称必须填写
            array('en_name', 'required','message'=>'-2','on'=>'create,update'),//城市英文必须填写
            array('province', 'required','message'=>'-3','on'=>'create,update'),//所属省份必须填写
            array('cn_name','checkName','on'=>'create'),//用户名已存在 -4【添加】
            array('cn_name','checkUSName','on'=>'update'),//用户名已存在 -5【修改】
            array('en_name','checkEnName','on'=>'create'),//用户名已存在 -6【添加】
            array('en_name','checkUSEnName','on'=>'update'),//用户名已存在 -7【修改】

			array('status', 'numerical', 'integerOnly'=>true),
			array('cn_name, en_name, province', 'length', 'max'=>50),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, cn_name, en_name, province, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'cn_name' => 'Cn Name',
			'en_name' => 'En Name',
			'province' => 'Province',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('cn_name',$this->cn_name,true);
		$criteria->compare('en_name',$this->en_name,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return CreateCity the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}


    public function getList($page,$rows,$order,$sort,$username){
        $db = Yii::app()->db->createCommand();
        $db->select('*');
        $db->from('{{create_city}}');
        if ($username) {
            $db->andWhere(array('like', 'cn_name', '%'.$username.'%'));
        }
        $db->order($sort.' '.$order);
        $db->limit($rows, ($rows * ($page - 1)));
        $info = $db->queryAll();
        foreach($info as $key=>$value){
            $info[$key]['status'] = $value['status'] ? '<i  onclick="city_tool.setStatus(\''.$value['id'].'\',0)"  style="padding: 6px;" class="icon-bullet_tick"></i>' : '<i  onclick="city_tool.setStatus(\''.$value['id'].'\',1)"  class="icon-bullet_cross"  style="padding: 6px;" ></i>';
        }
        return array(
            'rows'=>$info ? $info : '',
            'total'=>$this->getTotal($username)
        );
    }
    public function create($cn_name,$en_name,$province){
        $city = new CreateCity();
        $city->setScenario('create');
        $cn_name = CHtml::encode($cn_name);
        $en_name = CHtml::encode($en_name);
        $province = CHtml::encode($province);
        $data = array(
            'cn_name'=>$cn_name,
            'en_name'=>$en_name,
            'province' => $province,
            'status'=>0
        );
        $city->setAttributes($data);
        if($city->save()){
            $id = $city->attributes['id'];
            return 1;
        }else{
            return Xtutuquan::returnError($city->getErrors());
        }
    }





    private function getTotal($username){
        $db = Yii::app()->db->createCommand();
        $db->select('COUNT(*) AS count');
        $db->from('{{create_city}}');
        if ($username) {
            $db->andWhere(array('like', 'cn_name', '%'.$username.'%'));
        }
        $info = $db->queryRow();
        return $info['count'];
    }
    public function checkName($attribute,$params){
        $info = $this->findByAttributes(array('cn_name'=>$this->cn_name));
        if($info){
            $this->addError($attribute,'-4');
        }
    }

    public function checkUSName($attribute,$params){
        $uinfo = CreateCity::model()->findByPk($this->id);
        $info = $this->findByAttributes(array('cn_name'=>$this->cn_name));
        if($this->cn_name != $uinfo->cn_name && $this->cn_name ){
            if($info){
                $this->addError($attribute,'-5');
            }
        }
    }
    public function checkEnName($attribute,$params){
        $info = $this->findByAttributes(array('en_name'=>$this->en_name));
        if($info){
            $this->addError($attribute,'-6');
        }
    }

    public function checkUSEnName($attribute,$params){
        $uinfo = CreateCity::model()->findByPk($this->id);
        $info = $this->findByAttributes(array('en_name'=>$this->en_name));
        if($this->en_name != $uinfo->en_name && $this->en_name ){
            if($info){
                $this->addError($attribute,'-7');
            }
        }
    }
    //删除
    public function remove($ids){
        return $this->deleteAll('id in('.$ids.')');
    }
    public function getCity($id){
        $db = Yii::app()->db->createCommand();
        $db->select('*');
        $db->from('{{create_city}}');
        $db->andWhere('id=:id',array(':id'=>$id));
        $info = $db->queryRow();
        return $info;
    }
    public function edit($id,$cn_name,$en_name,$province){
        $info = CreateCity::model()->findByPk($id);
        $info->setScenario('update');
        $cn_name = CHtml::encode($cn_name);
        $en_name = CHtml::encode($en_name);
        $province = CHtml::encode($province);
        $data = array();
        $data['cn_name'] = $cn_name;
        $data['en_name'] = $en_name;
        $data['province'] = $province;
        $info->setAttributes($data);
        if($info->save()){
            return 1;
        }else{
            return Xtutuquan::returnError($info->getErrors());
        }
    }
}
